package main.leetcode.offer.secondround.from03to50;

/** 04.二维数组中的查找 */
public class ex04 {
    public static void main(String[] args) {
        int[][] nums = {
            {1, 4, 7, 11, 15},
            {2, 5, 8, 12, 19},
            {3, 6, 9, 16, 22},
            {10, 13, 14, 17, 24},
            {18, 21, 23, 26, 30}
            //      {1, 4, 5}, {2, 5, 6}, {3, 7, 9}
        };
        System.out.println(new ex04().findNumberIn2DArray(nums, 7));
    }

    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        int m = matrix.length;
        if (m == 0) return false;
        int n = matrix[0].length;
        if (n == 0) return false;
        int i = 0, j = n - 1;
        while (matrix[i][j] != target) {
            if (matrix[i][j] < target) {
                ++i;
            } else if (matrix[i][j] > target) {
                --j;
            }
            if (i > m - 1 || j < 0) return false;
        }
        return true;
    }
}
